//
//  JMCurveWreckerViewController.m
//  VoluntaryStar
//
//  Created by ios on 15/8/27.
//  Copyright (c) 2015年 ios. All rights reserved.
//

#import "JMCurveWreckerViewController.h"
#import "JMBrowserViewController.h"
#import "JMCoustmButton.h"
#import "JMStudyCircleViewController.h"
#import "JMLoginViewController.h"


@interface JMCurveWreckerViewController ()

@property (nonatomic,strong) UIImageView *backImageView;

@property (nonatomic,strong) UIButton *topImageView;

@property (nonatomic,strong) NSMutableArray *itemButtonArray;

@property (nonatomic, strong) UIImageView *lineImageView;

@end

@implementation JMCurveWreckerViewController

#pragma mark  创建界面 控件

-(UIImageView *)backImageView{
    if(!_backImageView){
        _backImageView = [[UIImageView alloc]init];
        _backImageView.backgroundColor = [UIColor clearColor];
        _backImageView.image = [UIImage imageNamed:@"ship.bundle/main_backImage"];
        _backImageView.frame = CGRectMake(0, 0, UI_SCREEN_WITH, UI_SCREEN_HEIGHT);
    }
    return _backImageView;
}
-(UIButton *)topImageView{
    if(!_topImageView){
        _topImageView = [[UIButton alloc]init];
        [_topImageView setImage:[UIImage imageNamed:@"secondLevel.bundle/xueba_topImage"] forState:UIControlStateNormal] ;
        _topImageView.backgroundColor = [UIColor clearColor];
        if(iphone4){
         _topImageView.frame = CGRectMake((UI_SCREEN_WITH -  UI_SCREEN_WITH *0.53125 *0.75)/2.0, 30, UI_SCREEN_WITH *0.53125 * 0.75, UI_SCREEN_WITH *0.75 *0.75);
        }else{
            _topImageView.frame = CGRectMake((UI_SCREEN_WITH -  UI_SCREEN_WITH *0.53125)/2.0, 30, UI_SCREEN_WITH *0.53125, UI_SCREEN_WITH *0.75);
        }
        [_topImageView addTarget:self action:@selector(itemButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _topImageView;
}

- (UIImageView *)lineImageView
{
    if (!_lineImageView) {
        UIImageView *imageView = [[UIImageView alloc]init];
        imageView.image = [UIImage imageNamed:@"secondLevel.bundle/xian"];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.backgroundColor = [UIColor clearColor];
        [self.view addSubview:_lineImageView  = imageView];
    }
    return _lineImageView;
}

-(NSMutableArray *)itemButtonArray{
    if(!_itemButtonArray){
        _itemButtonArray = [[NSMutableArray alloc]initWithCapacity:0];
        NSArray *array = @[@"secondLevel.bundle/cicle_image"];
        for(int i =0 ;i <array.count;i++){
            JMCoustmButton *btn = [[JMCoustmButton alloc]initWithFrame:CGRectMake(0, 0, 0, 0) WithNormalImage:array[i] SelectedImage:@""];
            btn.tag = i;
            [btn addTarget:self action:@selector(itemButtonClick:) forControlEvents:UIControlEventTouchUpInside];
            [_itemButtonArray addObject:btn];
        }
    }
    return _itemButtonArray;
}
#pragma mark viewWillAppear

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    self.navigationController.navigationBarHidden = YES;
    self.navBackBtn.hidden= YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.backImageView];
    self.lineImageView.frame = CGRectMake(UI_SCREEN_WITH/2.0 -0.5, self.topImageView.frameBottom - 25, 0.8,  UI_SCREEN_WITH * 0.225 );

    [self.view addSubview:self.topImageView];
    [self setItemButtonFrame];
}
-(void)setItemButtonFrame{
    
    
    for(int  i =0 ;i<self.itemButtonArray.count;i++){
        JMCoustmButton *btn = self.itemButtonArray[i];
        btn.tag = 1;
        if(iphone4){
            btn.frame = CGRectMake(UI_SCREEN_WITH/2.0 - UI_SCREEN_WITH *0.3375* 0.75/2.0, self.topImageView.frameBottom + UI_SCREEN_WITH * 0.125,UI_SCREEN_WITH *0.3375 * 0.75, UI_SCREEN_WITH *0.353125* 0.75);
        }else{
            btn.frame = CGRectMake(UI_SCREEN_WITH/2.0 -UI_SCREEN_WITH *0.3375/2.0, self.topImageView.frameBottom + UI_SCREEN_WITH * 0.125,UI_SCREEN_WITH *0.3375, UI_SCREEN_WITH *0.353125);
        }
        [self.view addSubview:btn];
        
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
#pragma mark  item 点击 进入 网页
-(void)itemButtonClick:(JMCoustmButton *)sender{
    JMConfigure *cf = [JMConfigure sharedConfigureManger];
    if(![cf.user.state isEqualToString:@"1"]){
        [self gotoLogin];
        return;
    }
    
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
    [dict setObject:cf.user.userId forKey:@"user"];
    [dict setObject:cf.user.token forKey:@"token"];
    [JMAppNetworkClient token_WithInfo:dict complete:^(id data, NSError *error) {
        if([data[@"status"] integerValue] == 1){
            if(sender.tag == 1){
                    JMStudyCircleViewController *vc = [JMStudyCircleViewController new];
                    [self.navigationController pushViewController:vc animated:YES];
                }else{
                    JMBrowserViewController *vc =[JMBrowserViewController new];
                    vc.url =[NSURL URLWithString:[JMAppNetworkClient awarInTheEndUrl]];
                    [self.navigationController pushViewController:vc animated:NO];
                }
            }else{
                [JMTools showAlertView:self.view message:data[@"message"]];
                [self gotoLogin];
            }
    }];
}
-(void)gotoLogin{
    JMLoginViewController *vc = [JMLoginViewController new];
    JMNavigationController *nav = [[JMNavigationController alloc]initWithRootViewController:vc];
    [self presentViewController:nav animated:YES completion:^{
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
